<?php
namespace app\Manage\controller;

use app\common\controller\Manage;

use app\common\model\ManageRoleRel;
use app\common\model\ManageRole;
use app\common\model\ManageRoleOperationRel;
use think\facade\Log;

use Request;


class Role extends Manage
{
    public function index()
    {
        if(Request::isAjax()){
            $manageRoleModel = new ManageRole();
            $data = input('param.');
            return $manageRoleModel->tableData($data);
        }else{
            return $this->fetch('index');
        }
    }

    public function add()
    {
        $this->view->engine->layout(false);
        $manageRoleModel = new ManageRole();
        if(Request::isPost()){
            if(!input('?param.name')){
                return error_code(11070);
            } 

            // 判断是否有同名的角色
            $data = input('param.');
            // 判断是否存在相同的设备场景
            $info = $manageRoleModel->where(['name'=>$data['name']])->find();
            if($info){
                return [
                    'status' => false,
                    'data' => '',
                    'msg' => '已存在同名角色！'
                ];  
            } else {
                $data['name'] = input('param.name');
                $manageRoleModel->save($data);
                
                \think\facade\Request::token('__ZlServer_Token__', 'sha1');
                return [
                    'status' => true,
                    'data' => '',
                    'msg' => '添加成功'
                ];
            }
        }
        return $this->fetch('edit');
    }
    
    public function del()
    {
        if(!input('?param.id')){
            return error_code(10000);
        }

        // id 在 5以内为保留角色不允许删除
        // if(input('param.id') <= 5){
        //     return error_code(11074);
        // }

        $ManageRoleModel = new ManageRole();
        return $ManageRoleModel->toDel(input('param.id'));

    }

    public function getOperation()
    {
        $return_data = [
            'code' => 0,
            'msg'  => '获取成功',
            'data' => [],
        ];

        if (!input('?param.id')) {
            return error_code(10000);
        }
        
        // id 在 5以内为保留角色不允许删除
        // if(input('param.id') <= 5){
        //     return error_code(11075);
        // }

        $manageRoleModel = new ManageRole();
        $re              = $manageRoleModel->getRoleOperation(input('param.id/d'));
        if (!$re['status']) {
            return $re;
        }
        $return_data['data'] = $re['data'];
        return $return_data;
    }

    public function savePerm(){
        $post = input('param.');

        if(!isset($post['id'])){
            return error_code(10000);
        }
        // 前台什么都不选
        // if(!isset($post['data'])){
        //     return error_code(10000);
        // }
        //保存角色信息
        $manageRoleModel = new ManageRole();
        $manageRoleInfo = $manageRoleModel->where(['id'=>$post['id']])->find();
        if(!$manageRoleInfo){
            return error_code(11071);
        }
        $mrorModel = new ManageRoleOperationRel();

        $mrorModel->savePerm($post['id'],$post['data']);

        // 这里需要清理所有关联该角色是用户缓存
        $manageRoleRelModel = new ManageRoleRel();
        $manageRoleRelModelList  = $manageRoleRelModel->where('role_id', $post['id'])->select();
        $manageRoleRelModelArray = $manageRoleRelModelList->toArray();

        foreach ($manageRoleRelModelArray as $k => $v) {
            // 这里需要清理缓存
            if(cache('?manage_operation_'.$v['manage_id'])){
                cache('manage_operation_'.$v['manage_id'], null);
            }
        }

        return [
            'status' => true,
            'data' => '',
            'msg' => '设置成功'
        ];
    }
}